<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="maintainerEditDialogFormRef" :rules="rules" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="姓名" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="tel">
							<el-input v-model="ruleForm.tel" placeholder="请输入联系电话" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { MaintainerVo } from './maintainer';
import { useUserInfo } from '/@/stores/userinfo';
import { ElMessage } from 'element-plus';
import { get } from 'lodash-es';
import { useMaintainer } from './maintainer';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const { userInfo } = useUserInfo();

// 定义变量内容
const maintainerEditDialogFormRef = ref();
const ruleForm = reactive<MaintainerVo>({
	id: '',
	name: '', // 公司名称
	tel: '', // 公司电话
	dealerId: '',
	companyId: '',
	branchId: '',
	account: '', // 账号
	password: '', // 密码
});
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const rules = ref({
	name: [
		{ required: true, type: 'string', message: '姓名不能为空', trigger: 'blur' },
		{ pattern: /[\u4e00-\u9fa5A-Za-z0-9]/, min: 2, max: 20, message: '请输入2-20个中英文字符名称', trigger: 'blur' },
	],
	tel: [
		{ required: true, type: 'string', message: '电话不能为空', trigger: 'blur' },
		{ pattern: /[0-9\-（）()]+/, type: 'string', message: '请输入正确的号码格式', trigger: 'blur' },
	],
});

// 打开弹窗
const openDialog = (row) => {
	setFormData(row);
	state.dialog.title = '修改信息';
	state.dialog.submitTxt = '修 改';
	state.dialog.isShowDialog = true;
};
const setFormData = (row) => {
	ruleForm.id = row.id;
	ruleForm.name = row.name;
	ruleForm.tel = row.tel;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (!maintainerEditDialogFormRef.value) return;
	if (get(userInfo, 'org.company.id') && get(userInfo, 'org.dealer.id') && get(userInfo, 'org.branch.id')) {
		ruleForm.companyId = userInfo.org.company.id;
		ruleForm.dealerId = userInfo.org.dealer.id;
		ruleForm.branchId = userInfo.org.branch.id;
		maintainerEditDialogFormRef.value.validate((valid) => {
			if (valid) {
				useMaintainer()
					.updateMaintainer(ruleForm)
					.then(() => {
						closeDialog();
						emit('refresh');
					});
			}
		});
	} else {
		ElMessage.error('获取用户经销商信息失败！');
	}
	// closeDialog();
	// emit('refresh');
	// if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
